package org.cid54.jshopper.controller;

import java.util.List;
import org.cid54.jshopper.model.UserGroup;
import org.cid54.jshopper.service.HomeService;
import org.cid54.jshopper.service.UserAndGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author fgo
 */
@Controller
@RequestMapping("home")
@SessionAttributes({"userName"})
public class HomeController {

    @Autowired
    private HomeService homeService;
    @Autowired
    private UserAndGroupService userAndGroupService;

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView doGet(@ModelAttribute("userName") String userName) {
        ModelAndView mav = new ModelAndView("home");
        
        List<UserGroup> userGroups = this.userAndGroupService.getUserGroupsByUserName(userName);

        mav.addObject("shoppingLists", this.homeService.getShoppingListByUserGroups(userGroups));

        return mav;
    }
}
